<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends La_Controller {

	function __construct() {
		parent::__construct();
		$this->load->helper('url');
		$this->load->library('utility');
		$this->load->model("User_model");
	}
	/**
	 * 注册
	 */
	function register() {
		$data = array('page_title' => '注册-行动网');
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->load->library('form_validation');
			
			$this->form_validation->set_rules('nickname', '用户名', 'required');
			$this->form_validation->set_rules('email', '邮箱', 'required');
			$this->form_validation->set_rules('password', '密码', 'required');
			$this->form_validation->set_rules('captcha', '确认码', 'required');
			if ($this->form_validation->run() == FALSE)
			{
				$this->load->view('user/register', $data);
			}
			$email = $this->input->post('email');
			$from = $this->input->post("from");
			$nickname = $this->input->post("nickname");
			$password = $this->input->post("password"); 
			$captcha = $this->input->post("captcha");
			$confirm_password = $this->input->post('confirm_password');
			$data = array('nickname' => $nickname, 'email' => $email, 'password' => $password, 'gender' => 0);
			if ($email && $nickname && $password) {
				if ($password !== $confirm_password) {
					if ($from == 'ajax') {
						echo "3";die(0);
					}
					$data['captcha'] = $this->utility->captcha();
					$_SESSION['flash_message'] = "密码与确认密码不一致";
					$this->load->view('user/register', $data);
					return;
				}
				if (strtolower($captcha) != strtolower($_SESSION['captcha'])) {
					if ($from == 'ajax') {
						echo "4";die(0);
					}
					$_SESSION['flash_message'] = "验证码不正确";
					$data['captcha'] = $this->utility->captcha();
					$this->load->view('user/register', $data);
					return;
				} else {
					unset($_SESSION['captcha']);
				}
				if (!$this->User_model->is_user_exist($email, $nickname)) {
					
					$data['active_code'] = md5($this->config->item('encryption_key') . date("YmdHis"));
					$user_id = $this->User_model->add($data);
					$this->load->library('utility');
					$email_content = '
					亲爱的' . $nickname .'：<br/>欢迎加入行动网!<br/>请点击下面的链接完成注册：<a href="' . site_url('/user/active/' . $user_id. "/" . $data['active_code']) . '">' 
					. site_url('/user/active/' . $user_id. "/" . $data['active_code']) .
					'</a><br/>如果以上链接无法点击，请将上面的地址复制到你的浏览器(如IE)的地址栏进入行动网。<br/>--(这是一封自动产生的email，请勿回复。)
					';
					$this->utility->send_mail('ixing115@gmail.com', $data['email'], '行动网激活邮件', $email_content);
					
					$post_data['msg_to_id'] = $user_id;
					$post_data['is_system'] = 1;
					$post_data['subject'] = "欢迎来行动！";
					$post_data['content'] = "尊敬的用户： <br/>
          感谢您来行动！  <br/>
          行动网旨在为身怀技能的人提供更多施展才华的机会， 同样，当您在工作和生活中遇到的难题的时候，相信在这里能够找到您需要的帮手。  <br/>
         如果您需要更多帮助，请到行动指南小组  <br/>
         我们欢迎您对行动网提出自己的建议，以促进我们更好的为您服务。  <br/>
          祝您的行动之旅愉快！ <br/> ";
					$post_data['msg_from_id'] = 0;
					$post_data['msg_from'] = '行动网';
					$post_data['created'] = date("Y-m-d H:i:s");
					$post_data['related_id'] = 0;
					$this->load->model('Message_model');
					$this->Message_model->add($post_data);
					
					if ($from == 'ajax') {
						echo "1";die(0);
					}
					$this->load->view('user/register_success');
				} else {
					if ($from == 'ajax') {
						echo "2";die(0);
					}
					la_redirect("user/register", "用户已经存在，请重新选择用户名和邮箱");
				}
			} 
		}else {
			$data['captcha'] = $this->utility->captcha();
			$this->load->view('user/register', $data);
		}
	}
	/**
	 * 登录
	 */
	function login() {
		$data = array('title' => '登录-行动网');
		if ( $this->input->post('user_id')) {
			$ref = $this->input->get("ref");
			if (empty($ref)) {
				$ref = site_url("");
			}
			$login_result = $this->authorize->login_routine();
			$from = $this->input->post("from");
			if ($from == 'ajax') {//ajax登录处理
				echo $login_result;die(0);
			}
			if ($login_result == 1) {
					redirect($ref);
					die(0);
			} else if ($login_result == 2) {
				$data['error_message'] =  "用户不存在";
			} else if ($login_result == 3) {
				$data['error_message'] =  "密码为空";
			} else if ($login_result ==4) {
				$data['error_message'] =  "帐户还未激活，请登录邮箱，找到邮件中激活地址，激活";
			} else if ($login_result == 5) {
				$data['error_message'] = "你已经被列入黑名单，禁止用此用户访问";
			} else if ($login_result == 6) {
				$data['error_message'] = "用户已经被注销";
			}
			 else {
				$data['error_message'] = "密码错误";
			}
			$data['nickname'] = $this->input->post('user_id');
		}
		
		if ($this->input->cookie('nickname')) {
			$data['nickname'] = $this->input->cookie('nickname');
		}
		if ($this->input->cookie('pass')) {
			$data['pass'] = $this->input->cookie('pass');
		}
		$this->load->view('user/login', $data);
	}
	/**
	 * ajax用户登录
	 */
	function login_ajax() {
		
		$this->load->view('user/login_ajax', $this->data);
	}
	/**
	 * ajax用户注册
	 */
	function register_ajax() {
		$this->data['captcha'] = $this->utility->captcha();
		$this->load->view('user/register_ajax', $this->data);
	}
	/**
	 * 激活
	 */
	function active() {
		$data = array('title' => '用户激活-行动网');
		if ($this->input->post("active")) {
			$user_id = (int)($this->uri->segment(3));
			$active_code = $this->uri->segment(4);
			$data = array();
			$data['gender'] = $this->input->post('gender');
			$data['birth_period'] = $this->input->post('birth_period');
			$data['province_id'] = $this->input->post('province');
			$data['city_id'] = $this->input->post('city');
			$data['area_id'] = $this->input->post('area');
			if ($this->User_model->is_user_active($user_id)) {
				la_redirect("user/login", "此用户已经激活");
				die(0);
			}
			if ($this->User_model->active($user_id, $active_code, $data)) {
				$user_info = $this->User_model->find_by_id($user_id);
				if (empty($user_info)) {
					la_redirect("user/login", "此用户不存在");
					die(0);
				}
				$credentials = array('user_id' => $user_id, 'logged_in' => 1, 'nickname' => $user_info['nickname']);
				$this->session->sess_destroy();
				$this->session->set_userdata($credentials);
				la_redirect("user/show", "激活成功");
			} else {
				la_redirect("user/login", "激活失败");
			}
			
		} else {
			$this->load->model("City_model");
			$data = array('page_title' => '行动网-激活帐号');
			$data['provinces'] = $this->City_model->find_all_province();
			$this->load->view('user/active', $data);
		}
	}
	/**
	 * 退出
	 */
	function logout() {
		$this->authorize->logout();
		redirect(site_url());
	}
	/**
	 * 忘记密码
	 */
	function forget_password() {
		$data = array('title' => '行动网-忘记密码');
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$email = $this->input->post('email');
			
//			$new_password = substr(md5("YmdHis" . rand(1,100)), 0, 8);
//			$this->User_model->change_password($email , $new_password);
//			$this->load->library('utility');
			$user = $this->User_model->find_user_by_email($email);
			$data['email'] = $email;
			if (empty($user)) {
				$data['result'] = 0;
			} else {
				$data['result'] = 1;
				$code = md5($this->config->item('encryption_key') . date("YmdHis"));
				$this->User_model->update_code($code, $user['id']);
				$link = site_url() . "user/reset_password?code=" . $code . "&id=" . $user['id'];
				$email_content = '你已经申请重置密码，请使用以下地址修改密码。<br/>' . $link;
				$this->utility->send_mail('ixing115@gmail.com', $email, '密码重置邮件', $email_content);
			}
			$this->load->view("user/forget_password_tips", $data);
		} else {
			$this->load->view('user/forget_password');
		}
	}
	
	
	/**
	 * 重置密码
	 */
	function reset_password() {
		$data = array('title' => '行动网-忘记密码');
		
		
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$id = $this->input->post('id');
			$code = $this->input->post("code");
			$password = $this->input->post("password");
			$confirm_password = $this->input->post("confirm_password"); 
			$user = $this->User_model->find_by_id($id);
			if($user['active_code'] != $code) {
				la_redirect("/user/login", "非法操作");
			}
			
			if ($password != $confirm_password) {
				la_redirect("/user/reset_password?id=" . $id . "&code=" . $code, "两次输入的密码不一样");
			}
			$this->User_model->change_password($id , $password);
			la_redirect("/user/login", "密码修改成功");
		} else {
			$id = $this->input->get("id");
			$code = $this->input->get("code");
			$user = $this->User_model->find_by_id($id);
			if($user['active_code'] != $code) {
				la_redirect("/user/login", "非法操作");
			}
			$data['id'] = $id;
			$data['code'] = $code;
			$this->load->view('user/reset_password', $data);
		}
	}
	
	/**
	 * 消息
	 */
	function messages() {
		$data = array('title' => '行动中心-行动网');
		$offset = intval($this->uri->segment(3));
		$user_id = $this->authorize->current_user();
		if (empty($user_id)) {
			la_redirect("user/login", '请先登录');
			die(0);
		}
		$this->_user_info($user_id);
		$this->load->model("Message_model");
		$type = $this->input->get("type");
		if (empty($type)) {
			$type = "unread";
		}
		$this->load->library('pager');
		$config['base_url'] = site_url("user/messages");
		$config['suffix'] = "?type=" . $type;
		$config['per_page'] = LA_PAGE_SIZE;
		$config['uri_segment'] = 3;
		switch ($type) {
			case "system":
				$this->data['message_count'] = $this->Message_model->get_recv_count($user_id, 1);
				$this->data['messages'] = $this->Message_model->find_recv_messages($user_id, $offset, $config['per_page'], 1);
				break;
			case "private":
				$this->data['message_count'] = $this->Message_model->get_recv_count($user_id);
				$this->data['messages'] = $this->Message_model->find_private_messages($user_id, $offset, $config['per_page']);	
				break;
			case "unread":
				$this->data['message_count'] = $this->Message_model->get_unread_count($user_id);
				$this->data['messages'] = $this->Message_model->find_unread_messages($user_id, $offset, $config['per_page']);
			default:
				break;
		}
		$config['total_rows'] = $this->data['message_count'];
		$this->pager->initialize($config);
		$this->data['user_nav'] = 'message';
		$this->data['type'] = $type;
		$this->data['pager'] =  $this->pager->create_links();
		$this->load->view('user/messages', $this->data);
	}
	//ajax 调用返回用户未读消息数量
	function ajax_message_count() {
		$this->load->model("Message_model");
		$user_id = intval($this->input->get("user_id"));
		$message_count = $this->Message_model->get_unread_count($user_id);
		if ($message_count > 0) {
			echo '<a href="/user/messages?type=unread" class="orange">(' . $message_count . ')</a>';
		} 
		exit(0);
	}
	
	function ajax_user_check() {
		$type = $this->input->get("type");
		$value = $this->input->get("value");
		switch ($type) {
			case 'nick':
				$result = $this->User_model->find_user_by_nick($value);
				break;
			case 'email':
			default:
				$result = $this->User_model->find_user_by_email($value);
				break;
		}
		echo empty($result) ? "{result:0}" : "{result:1}";
		die(0);
	}
	/**
	 * 评价用户
	 */
	function evaluate() {
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$post_data = array();
			$post_data['target_id'] = $this->input->post('target_id');
			$post_data['related_id'] = $this->input->post('related_id');
			$post_data['target_type'] = $this->input->post('target_type');
			$post_data['content'] = $this->input->post('content');
			$post_data['created_by'] = current_user();
			$post_data['created'] = date("Y-m-d H:i:s");
			$post_data['score'] = $this->input->post('score');;
			$this->load->model('Comment_model');
			$this->Comment_model->add($post_data);
			la_redirect('story/show/' . $post_data['related_id'], "评价成功");
			
		} else {
			$user_id = $this->input->get("user_id");
			$story_id = $this->input->get("story_id");
			$this->load->model('Emergency_model');
			if (!$this->authorize->is_logged_in()) {
				la_redirect('user/login?ref=' . urlencode(site_url('user/evaluate?user_id=' . $user_id)), "请你先登录");
			}
			$user = $this->User_model->find_by_id(intval($user_id));
			if (empty($user['id'])) {
				la_redirect(site_url(), '用户不存在');
			} else {
				$this->data['user'] = $user;
				$this->data['story'] = $this->Emergency_model->find_by_id($story_id);
				$this->data['target_type'] = 'user';
				$this->data['target_id'] = $user['id'];
			}
		}
		$this->load->view('user/evaluate', $this->data);
	}
	/**
	 * 消息查看
	 */
	function message_view() {
		$data = array('title' => '消息查看-行动网');
		$user_id = $this->authorize->current_user();
		if (empty($user_id)) {
			la_redirect("user/login", "请你先登录");
		}
		$message_id = intval($this->uri->segment(3));
		$this->load->model('Message_model');
		$message = $this->Message_model->find_by_id($message_id);
		$group_array = array(-1);
		$is_v = $this->User_model->is_vertification($user_id);
		
		$is_expert = $this->User_model->is_expert_vertification($user_id);
		if ($is_expert) {
			$group_array[] = -3;
		}
		if ($is_v) {
			$group_array[] = -2;
		}
		
		if (!$is_v && !$is_expert) {
			$group_array[] = -4;
		}
		if ($message['msg_to_id'] != $user_id && !in_array($message['msg_to_id'], $group_array)) {
			la_redirect("error/noright", "你无权查看此页面");
		}
		$this->Message_model-> set_as_readed($message['id']);
		$this->data['user_nav'] = 'message';
		$this->_user_info($user_id);
		$this->data['message'] = $message;
		if ($message['is_system'] == 1) {
			$this->data['type'] = 'system'; 
		} else {
			$this->data['type'] = 'private'; 
		}
		$this->load->view('user/message_view', $this->data);
	}
	/**
	 * 消息创建
	 */
	function message_create() {
		$user_id = $this->input->get("user_id");
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) {
			la_redirect("/user/login?ref=/user/message_create?user_id=". $user_id, "请先登录");
			exit(0);
		}
		$this->page_title("发送消息-行动网");
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$post_data = array();
			$msg_to = $this->input->post('msg_to');
			$msg_to_array = split(";", $msg_to);
			if(empty($msg_to_array)) {
				redirect("/user/message_create?user_id=". $user_id);
			}
			$this->load->model('Message_model');
			foreach ($msg_to_array as $to) {
				$user_to = $this->User_model->find_user_by_nick($to);
				if(empty($user_to)) {
					continue;
				}
				$post_data['msg_to_id'] = $user_to['id'];
				$post_data['subject'] = $this->input->post('subject');
				$post_data['content'] = $this->input->post('content');
				$post_data['msg_from_id'] = $this->authorize->current_user();
				$post_data['msg_from'] = current_user("nickname");
				$post_data['created'] = date("Y-m-d H:i:s");
				$post_data['related_id'] = $this->input->post('related_id');
				
				$this->Message_model->add($post_data);
			}
			la_redirect("user/messages", "发表成功");
		} else {
			$this->load->model('Friend_model');
			$to_user = $this->User_model->find_by_id($user_id);
			$this->data['user_nav'] = 'message';
			
			$this->_user_info($current_user_id);
			$this->data['friends'] = $this->Friend_model->find_friends($current_user_id);
			$this->data['to_user'] = $to_user;
			$this->load->view('user/message_create', $this->data);
		}
	}
	/**
	 * 删除消息
	 */
	function message_delete() {
		$user_id = $this->authorize->current_user();
		if (empty($user_id)) {
			la_redirect("user/login", "请你先登录");
		}
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$this->load->model('Message_model');
			$msg_ids = $this->input->post('msg_id');
			if (!empty($msg_ids)) {
				foreach($msg_ids as $msg_id) {
					$message = $this->Message_model->find_by_id($msg_id);
					if ($message['msg_to_id'] != $user_id) {
						continue;
					}
					$this->Message_model->delete($msg_id);
				}
			}
			redirect("/user/messages");
		}
	}
	/**
	 * 行动中心/个人空间 跳转
	 */
	function show() {
		$user_id = $this->input->get("user_id");
		$current_user_id = $this->authorize->current_user();
		if (empty($user_id)) {
			$user_id = $current_user_id;
		}
		$is_preview = $this->input->get("preview");
		if ($user_id == $current_user_id && empty($is_preview)) {
			redirect("/user/messages");
		} else {
			$url = "/user/experts?user_id=" . $user_id . (!empty($is_preview) ? '&preview=1' : '');
			redirect($url);
		}
		
	}
	/**
	 * 我的行家页面
	 */
	function experts() {
		$is_preview = $this->input->get("preview");
		if (!empty($is_preview)) {
			$this->data['is_preview'] = $is_preview;
		}
		$user_id = $this->input->get("user_id");
		$current_user_id = $this->authorize->current_user();
		if (empty($user_id)) {
			$user_id = $current_user_id;
		}
		$this->_user_info($user_id);
		$this->page_title($this->data['ta'] . "的行家-行动网");
		$expert_id = $this->input->get("expert_id");
		$this->data['user_nav'] = 'expert';
		$this->load->model(array('Expert_model', "User_card_model"));
		$this->data['experts'] = $this->Expert_model->find_by_user_id($user_id);
		if (empty($expert_id) ) {
			if( !empty($this->data['experts'])) {
				$this->data['current_expert_id'] = $this->data['experts'][0]['id'];
			} else {
				$this->data['current_expert_id'] = 0;
			}
		} else {
			$this->data['current_expert_id'] = $expert_id;
		}
		$this->data['expert'] = $this->Expert_model->find_by_id($this->data['current_expert_id']);
		
		$this->load->view('user/show', $this->data);
	}
	
	/**
	 * 我的事项
	 */
	function story() {
		$is_preview = $this->input->get("preview");
		if (!empty($is_preview)) {
			$this->data['is_preview'] = $is_preview;
		}
		$offset = intval($this->uri->segment(3));
		$user_id = $this->input->get("user_id");
		if (empty($user_id)) {
			$user_id = $this->authorize->current_user();
		}
		$type = $this->input->get("type");
		if (empty($type)) {
			$type = "apply";
		}
		if ($user_id != $this->authorize->current_user() && $type =='favourite') {
			$type = "create";
		}
		$this->data['type'] = $type;
		$this->_user_info($user_id);
		$this->page_title($this->data['ta'] . "的事项-行动网");
		$this->data['user_nav'] = 'story';
		$this->data['type'] = $type;
		
		$this->load->library('pager');
		$config['base_url'] = site_url("user/story");
		$config['suffix'] = '?user_id=' . $user_id . "&type=" . $type;
		$config['per_page'] = LA_PAGE_SIZE;
		$config['uri_segment'] = 3;
		switch ($type) {
			case 'apply' :
				$this->load->model("User_emergency_model");
				$this->data['story_count'] = $this->User_emergency_model->get_count_applied_stories($user_id);
				$this->data['stories'] = $this->User_emergency_model->find_applied_stories($user_id, $offset, $config['per_page']);
			break;
			case 'favourite':
				$this->load->model("Favourite_model");
				$this->data['story_count'] = $this->Favourite_model->get_count_favourite_story($user_id);
				$this->data['stories'] = $this->Favourite_model->find_favourite_stories($user_id, $offset, $config['per_page']);
				break;
			case 'create':
			default:
				$this->load->model("Emergency_model");
				$this->data['story_count'] = $this->Emergency_model->get_count_by_condition(" g.status=1 and g.created_by=" .intval($user_id));
				$this->data['stories'] = $this->Emergency_model->find_by_condition(" g.status=1 and g.created_by=" .intval($user_id), $offset, $config['per_page']);
			break;
		
		}
		$config['total_rows'] = $this->data['story_count'];
		$this->pager->initialize($config);
		$this->data['pager'] =  $this->pager->create_links();
		$this->load->view('user/story', $this->data);
	}
	
	/**
	 * 我的小组
	 */
	function group() {
		$is_preview = $this->input->get("preview");
		if (!empty($is_preview)) {
			$this->data['is_preview'] = $is_preview;
		}
		$user_id = $this->input->get("user_id");
		$type = $this->input->get("type");
		if (empty($user_id)) {
			$user_id = $this->authorize->current_user();
		}
		if (empty($type)) {
			$type = 'create';
		}
		$this->load->model("Group_model");
		if ($type == 'join') {
			$groups = $this->Group_model->find_groups_user_join($user_id);
		} else {
			
			$groups = $this->Group_model->find_groups_by_created_by($user_id);
		}
		
		$this->data['groups'] = $groups;
		$this->data['user_nav'] = 'group';
		$this->data['sub_menu'] = $type;
		$this->_user_info($user_id);
		$this->page_title( $this->data['ta'] . "的合作组-行动网");
		$this->load->view('user/group', $this->data);
	}
	
	/**
	 * 我的评价
	 */
	function evaluate_result() {
		$is_preview = $this->input->get("preview");
		if (!empty($is_preview)) {
			$this->data['is_preview'] = $is_preview;
		}
		$offset = intval($this->uri->segment(3));
		$user_id = $this->input->get("user_id");
		if (empty($user_id)) {
			$user_id = $this->authorize->current_user();
		}
		$this->_user_info($user_id);
		$this->load->model('Comment_model');
		$this->data['score'] = floor($this->Comment_model->get_average_score($user_id, 'user'));
		$this->load->library('pager');
		$config['base_url'] = site_url("user/evaluate_result");
		$config['suffix'] = '?user_id=' . $user_id ;
		$config['per_page'] = LA_PAGE_SIZE;
		$config['uri_segment'] = 3;
		$this->data['evaluate_count'] = $this->Comment_model->get_count_by_target_id($user_id, "user");
		$this->data['evaluates'] = $this->Comment_model->find_by_target_id($user_id, "user", $offset, $config['per_page']);
		
		$config['total_rows'] = $this->data['evaluate_count'];
		$this->pager->initialize($config);
		$this->data['pager'] =  $this->pager->create_links();
		$this->data['user_nav'] = 'evaluate';
		$this->load->view('user/evaluate_result', $this->data);
	}
	/**
	 * 联系方式
	 */
	function contact() {
		$is_preview = $this->input->get("preview");
		if (!empty($is_preview)) {
			$this->data['is_preview'] = $is_preview;
		}
		$this->data['user_nav'] = 'contact';
		$user_id = $this->input->get("user_id");
		$this->_user_info($user_id);
		$this->page_title( $this->data['ta'] . "的联系方式-行动网");
		$contact = $this->User_model->find_contact_by_user_id($user_id);
		$current_user = $this->authorize->current_user();
		$this->data['is_vertification'] = $this->User_model->is_vertification($current_user);
		$this->data['contact'] = $contact;
		$this->load->view('user/address', $this->data);
	}
	
	public function edit_contact() {
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) {
			la_redirect("user/login?ref=/user/edit", "请你先登录");
		}
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$id = $this->input->post('id');
			$data['mobile'] = $this->input->post('mobile');
			$data['telephone'] = $this->input->post('telephone');
			$data['qq'] = $this->input->post('qq');
			$data['blog'] = $this->input->post('blog');
			$this->load->model("Contact_model");
			if (!empty($id)) {
				$this->Contact_model->update($data, $id);
			} else {
				$data['user_id'] = $current_user_id;
				$this->Contact_model->add($data);
			}
			la_redirect("/user/contact?user_id=" . $current_user_id, "修改联系方式成功");
		} else {
			$contact = $this->User_model->find_contact_by_user_id($current_user_id);
			$this->data['contact'] = $contact;
			$this->load->view('user/edit_address', $this->data);
		}
	}
	private function _user_info($user_id) {
		
		$user = $this->User_model->find_by_id($user_id);
		if (empty($user)) {
			la_redirect("", "用户不存在");
		}
		$this->load->model(array("Friend_model","Message_model", "User_card_model","City_model", "Comment_model", "Expert_model", "Category_model"));
		$this->data['first_level_categories'] = $this->Category_model->find_son_category(1);
		$this->data['provinces'] = $this->City_model->find_all_province();
		$current_user_id = $this->authorize->current_user();
		$this->data['user_info'] = $user;
		$is_preview = $this->input->get("preview");
		//加入一个预览状态
		if ($user_id == $current_user_id && !$is_preview) {
			$this->data['ta'] = "我";
			$this->data['is_author'] = 1;
			$this->data['card_count'] = $this->User_card_model->get_cards_count($current_user_id, 0);
		} else {
			$this->data['is_author'] = 0;
			if ($user['gender'] == 2) {
				$this->data['ta'] = "她";
			} else {
				$this->data['ta'] = "他";
			}
		}
		
		$this->data['expert_count'] = $this->Expert_model->get_count_by_user_id($user_id);
		$this->data['is_friend'] = $this->Friend_model->is_friend($current_user_id, $user_id);
		$this->data['published_story_count'] = $this->User_model->get_publish_story_count($user_id);
		$this->data['applied_story_count'] = $this->User_model->get_applied_stroy_count($user_id);
		$this->data['joined_group_count'] = $this->User_model->get_joined_group_count($user_id);
		$this->data['user_experts'] = $this->User_model->find_experts_by_user_id($user_id);
		$this->data['vertifications'] = $this->User_model->find_vertification_by_user_id($user_id); 
		$this->data['address'] = $this->User_model->find_user_address($user_id);
		$this->data['subscribes'] = $this->User_model->find_subscribes($user_id);
		$this->data['unread_message_count'] = $this->Message_model->get_unread_count($user_id);
		$this->data['score'] = floor($this->Comment_model->get_average_score($user_id, 'user'));
	}
	
	/**
	 * 身份验证
	 */
	public function vertification() {
		$user_id = $this->authorize->current_user();
		$this->page_title("用户验证-用户验证");
		$type = $this->input->get("type");
		if (empty($type)) {
			$type = 'id';
		} 
		$this->data['type'] = $type;

		if (empty($user_id)) {
			la_redirect("user/login?ref=" . site_url("user/vertification"), "请你先登录");
		}
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$post_data['obj_name'] = $this->input->post('obj_name');
			$post_data['obj_value'] = $this->input->post('obj_value');
			$post_data['mobile'] = $this->input->post('mobile');
			$post_data['created_by'] = $user_id;
			$post_data['created'] = date("Y-m-d H:i:s");
			$post_data['type'] = $this->input->post("type");
			$this->load->model('Vertification_model');
			$ver_id = $this->Vertification_model->add($post_data);
			$config['upload_path'] = './img/uploads/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '1024';
			$config['file_name'] = substr(md5(date("YmdHis")),0,16);
			$this->load->library('upload', $config);
			if ($this->upload->do_upload() ) {
				$pic_data = $this->upload->data();
				$this->Vertification_model->update(array('pic' => $pic_data['file_name']), $ver_id);
			}
			la_redirect("user/show", "发表成功");
		} else {
			if ($type == 'id') {
				$this->load->view('user/id_vertification', $this->data);
			} else {
				$this->load->view('user/expert_vertification', $this->data);
			}
		}
	}
	
	/**
	 * 加某人为好友
	 */
	public function add_friend() {
		$user_id = $this->input->get("user_id");
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) {
			la_redirect("user/login?ref=/user/add_friend?user_id=" . $user_id, "请你先登录");
		}
		$this->load->model("Friend_model");
		if (!$this->Friend_model->is_friend($current_user_id, $user_id)) {
			$this->Friend_model->add($current_user_id, $user_id);
			$msg = "添加好友成功";
		} else {
			$msg = "你们已经是好友";
		}
		la_redirect("/user/show?user_id=" . $user_id, $msg);
	}
	
	/**
	 * 取消某人为好友
	 */
	public function remove_friend() {
		$user_id = $this->input->get("user_id");
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) {
			la_redirect("user/login?ref=/user/remove_friend?user_id=" . $user_id, "请你先登录");
		}
		$this->load->model("Friend_model");
		$this->Friend_model->remove($current_user_id, $user_id);
		la_redirect("/user/show?user_id=" . $user_id, "取消好友成功");
	}
	/**
	 * 修改用户相关信息
	 */
	public function edit() {
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) {
			la_redirect("user/login?ref=/user/edit", "请你先登录");
		}
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			
		} else {
			
		}
	}
	
	/**
	 * 修改用户图像
	 */
	public function edit_avatar() {
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) {
			la_redirect("user/login?ref=/user/edit", "请你先登录");
		}
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$config['upload_path'] = './img/avatar/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '1024';
			$this->load->library('upload', $config);
			if ($this->upload->do_upload() ) {
				$pic_data = $this->upload->data();
				$this->User_model->update_avatar($current_user_id, $pic_data);
				$config['image_library'] = 'gd2';
				$config['source_image'] = ROOTPATH . '/img/avatar/' . $pic_data['file_name'];
				$config['create_thumb'] = TRUE;
				$config['maintain_ratio'] = false;
				$config['width'] = 120;
				$config['height'] = 120;
				$this->load->library('image_lib', $config);
				$this->image_lib->resize();
			} else {
				echo $this->upload->display_errors('<p>', '</p>');
			}
			la_redirect("/user/show", "修改个人图片成功");
		} else {
			$avatar = $this->User_model->find_avatar_by_user_id($current_user_id);
			$this->data['avatar'] = $avatar;
			$this->load->view('user/edit_avatar', $this->data);
		}
	}
	/**
	 * 订阅事项
	 */
	public function subscribe() {
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) { //未登录
			echo "0";
			die(0);
		}
		$this->page_title("订阅事项-行动网");
		$this->load->model("Subscribe_model");
		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			$province = $this->input->post('province');
			$city = $this->input->post('city');
			$area = $this->input->post('area');
			$category_id = $this->input->post('category_id');
			$this->Subscribe_model->add(array('created_by' => $current_user_id, 'province' => $province, 'city' => $city, 'area' => $area, 'category_id' => $category_id , 'created' => date("Y-m-d H:i:s")));
			echo "1";die(0);
		} else {
			//@todo libzhang
		}
	}
	/**
	 * 删除订阅
	 */
	public function delete_subscribe() {
		$id = intval($this->input->get("id"));
		$this->load->model("Subscribe_model");
		$subscribe = $this->Subscribe_model->find_by_id($id);
		$current_user_id = $this->authorize->current_user();
		
		if ($current_user_id != $subscribe['created_by']) {
			echo '0';
		} else {
			$this->Subscribe_model->remove($id);
			echo "1";
		}
		die(0);
	}
	
	public function reset_captcha() {
		$captcha = $this->utility->captcha();
		echo $captcha['image'];
		die(0);
	}
	
	public function ajax_update() {
		$type = $this->input->post("type");
		$val = $this->input->post("val");
		$current_user_id = $this->authorize->current_user();
		if (empty($current_user_id)) { //未登录
			echo "0";
			die(0);
		}
		if(!in_array($type, array("mobile", "telephone", "email", "qq", "blog"))) {
			echo "0";
		} else {
			$this->load->model("Contact_model");
			$data = array($type => $val);
			$this->Contact_model->update_by_userid($data, $current_user_id);
			echo "1";
		}
		die(0);
	}
}
